package com.kun.portal.webframe.service;
import com.kun.portal.webframe.dao.CourseNodeBaseDao;
import com.kun.portal.webframe.dao.CourseNodeDao;
import com.kun.portal.webframe.dao.FlightPlaneDao;
import com.kun.portal.webframe.po.CourseNodeBasePo;
import com.kun.portal.webframe.po.CourseNodePo;
import com.kun.portal.webframe.po.FlightPlanePo;
import com.kun.portal.webframe.util.MessageEmit;
import com.kun.portal.webframe.util.MessageFilter;
import com.kun.portal.webframe.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * Created by Administrator on 2018-09-06.
 */
@Service
public class ProcessMgrService {
    @Autowired
    private CourseNodeBaseDao courseNodeBaseDao;
    @Autowired
    private CourseNodeDao courseNodeDao;


    @Autowired
    private FlightPlaneDao flightPlaneDao;
    public List<CourseNodeBasePo> findAllCnb(){
        return courseNodeBaseDao.findAll();
    }
    public void saveCnb(CourseNodeBasePo courseNodeBasePo){
        courseNodeBaseDao.save(courseNodeBasePo);

    }
    public void deleteCnb(CourseNodeBasePo courseNodeBasePo){
        courseNodeBaseDao.delete(courseNodeBasePo);
    }

    @Transactional
    public CourseNodePo nodeCompleted(CourseNodePo courseNodePo){

        CourseNodePo finalCourseNodePo = courseNodePo;
        List<CourseNodePo> courseNodePos= courseNodeDao.findAll(new Specification<CourseNodePo>() {
            @Override
            public Predicate toPredicate(Root<CourseNodePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate= cb.equal(root.get("flightPlanePo"), finalCourseNodePo.getFlightPlanePo());
                Predicate predicate2= cb.equal(root.get("nodeName"), finalCourseNodePo.getNodeName());
                return  cb.and(predicate,predicate2);
            }
        });
        if(courseNodePos.size()>=1){
            courseNodePo =courseNodePos.get(0);
           // courseNodePo.getFlightPlanePo().setCourseNodePos(new HashSet<CourseNodePo>());
            return  courseNodePos.get(0);
        }
        if(courseNodePo.getNodeName().equals("上轮档")){
            FlightPlanePo flightPlanePo=courseNodePo.getFlightPlanePo();
            flightPlanePo=flightPlaneDao.findOne(flightPlanePo.getPlaneId());
            flightPlanePo.setProcessStatus(Status.ENSURE_START);
            flightPlaneDao.save(flightPlanePo);
        }
        if(courseNodePo.getNodeName().equals("撤轮档")){
            FlightPlanePo flightPlanePo=courseNodePo.getFlightPlanePo();
            flightPlanePo=flightPlaneDao.findOne(flightPlanePo.getPlaneId());
         //
            flightPlanePo.setProcessStatus(Status.ENSURE_END);
            flightPlaneDao.save(flightPlanePo);
        }
        courseNodePo=  courseNodeDao.save(courseNodePo);
        MessageEmit.emit(courseNodePo, MessageFilter.MessageType.CourseNodeCompleted);
        return courseNodePo;
    }
}
